package com.qws.plugin.presentation.flutter_presentation.ui

import android.app.Presentation
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.Display
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.MediaController
import android.widget.TextView
import android.widget.VideoView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.qws.plugin.presentation.flutter_presentation.R
import com.qws.plugin.presentation.flutter_presentation.model.Ad
import com.qws.plugin.presentation.flutter_presentation.model.CustomModel


class QwsPresentation(
    val outerContext: Context,
    display: Display,
    private var sModel: CustomModel
) :
    Presentation(outerContext, display) {

    private lateinit var videView: VideoView
    private lateinit var ivAd: ImageView
    private lateinit var tvPayAmt: TextView
    private lateinit var tvPayable: TextView
    private lateinit var tvNeetAmt: TextView
    private lateinit var rvProduct: RecyclerView
    private lateinit var lvProduct: LinearLayout
    private var mMediaController: MediaController? = null
    private var productAdapter: ProductAdapter = ProductAdapter()
    private var adIndex: Int = 0
    private val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            val ads = sModel.ads
            ads?.let {
                if (it.size == 0) {
                    return
                }
                val ad = it[++adIndex % it.size]
                if (ad.type == 0) {
                    showImage(ad)
                } else {
                    playVideo(ad)
                }
            }

            super.handleMessage(msg)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.presentation_layout)
        videView = findViewById(R.id.video)
        ivAd = findViewById(R.id.banner_view)
        tvPayAmt = findViewById(R.id.tv_pay_amt)
        tvPayable = findViewById(R.id.tv_payable)
        tvNeetAmt = findViewById(R.id.tv_need)
        rvProduct = findViewById(R.id.rv_product)
        lvProduct = findViewById(R.id.lv_product)

        rvProduct.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = productAdapter
        }
        loadData(sModel)
    }

    fun loadData(model: CustomModel) {
        sModel = model
        adIndex = 0
        tvPayAmt.text = sModel.payAmt
        tvPayable.text = sModel.payable
        tvNeetAmt.text = sModel.needAmt
        handler.removeMessages(0)
        if (model.fullScreen) {
            lvProduct.visibility = View.GONE
        } else {
            lvProduct.visibility = View.VISIBLE
        }
        productAdapter.submitList(model.products)
        model.ads?.let {
            if(it.size == 0) {
                return
            }
            val ad = it[adIndex]
            if (ad.type == 0) {
                showImage(ad)
            } else {
                playVideo(ad)
            }
        }
    }

    fun showImage(ad: Ad) {
        ivAd.visibility = View.VISIBLE
        videView.visibility = View.GONE
        Glide.with(ivAd).load(ad.url).into(ivAd)
        handler.sendEmptyMessageDelayed(0, 5000)
    }

    fun playVideo(ad: Ad) {
        ivAd.visibility = View.GONE
        videView.visibility = View.VISIBLE

        ad.cover?.let {
            ivAd.visibility = View.VISIBLE
            Glide.with(ivAd).load(it).into(ivAd)
        }
        mMediaController = MediaController(outerContext)
        mMediaController!!.visibility = View.GONE
        videView.setMediaController(mMediaController)
        val proxy = HttpProxy.get(context).getProxy()
        videView.setVideoPath(proxy.getProxyUrl(ad.url))
        videView.setOnCompletionListener {
            handler.sendEmptyMessage(0)
        }
        videView.setOnPreparedListener {
            ivAd.visibility = View.GONE
            videView.start()
        }
    }
}